      <h1 class="entry-title entry-title-single" itemprop="headline">ABAP 日期时间函数</h1>

    <div class="entry-content entry-content-single" itemprop="text">
      <p>&nbsp;<br />
<b><span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/sap-abap" title="ABAP" target="_blank">ABAP</a></span>日期时间<span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="函数" target="_blank">函数</a></span>收集：</b></p>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="269"><b><span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="函数" target="_blank">函数</a></span>名称</b>  (内页-点击名称可查看操作)</td>
<td valign="top" width="274"><b><span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="函数" target="_blank">函数</a></span>说明</b></td>
<td valign="top" width="170"><b>备注</b></td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#FIMA_DATE_CREATE">FIMA_DATE_CREATE</a></p>
<p><a href="#RP_CALC_DATE_IN_INTERVAL">RP_CALC_DATE_IN_INTERVAL</a></td>
<td valign="top" width="274">获取查询日期前、后的年、月、日</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#LAST_DAY_OF_MONTHS">LAST_DAY_OF_MONTHS</a></p>
<p><a href="#RP_LAST_DAY_OF_MONTHS">RP_LAST_DAY_OF_MONTHS</a></p>
<p><a href="#BKK_GET_MONTH_LASTDAY">BKK_GET_MONTH_LASTDAY</a></td>
<td valign="top" width="274">获取查询日期最后一天的日期</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#CCM_GO_BACK_MONTHS">CCM_GO_BACK_MONTHS</a></td>
<td valign="top" width="274">获取查询日期过去N月的日期</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
</tbody>
</table>
<p><span id="more-287"></span></p>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#MONTH_PLUS_DETERMINE">MONTH_PLUS_DETERMINE</a></td>
<td valign="top" width="274">获取查询日期未来N月的日期</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269">&nbsp;</td>
<td valign="top" width="274">&nbsp;</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#DATE_GET_WEEK">DATE_GET_WEEK</a></td>
<td valign="top" width="274">获取查询日期该年的第几周</td>
<td valign="top" width="170">输出格式：YYYYWW</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#WEEK_GET_FIRST_DAY">WEEK_GET_FIRST_DAY</a></td>
<td valign="top" width="274">获取查询周数的第一天</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#DAY_IN_WEEK">DAY_IN_WEEK</a></td>
<td valign="top" width="274">获取查询日期该天是星期几</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#GET_WEEK_INFO_BASED_ON_DATE">GET_WEEK_INFO_BASED_ON_DATE</a></td>
<td valign="top" width="274">获取查询日期该年的第几周和这周周一、周日日期</td>
<td valign="top" width="170">周起始日是周一</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#HR_GBSSP_GET_WEEK_DATES">HR_GBSSP_GET_WEEK_DATES</a></td>
<td valign="top" width="274">获取查询日期该年的第几周和这周周一、周日日期，该天是周几</td>
<td valign="top" width="170">周起始日是周日</td>
</tr>
<tr>
<td valign="top" width="269">&nbsp;</td>
<td valign="top" width="274">&nbsp;</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269">DATE_CHECK_PLAUSIBILITY</td>
<td valign="top" width="274">检查日期有效性</td>
<td valign="top" width="170">SY-SUBRC = 0 有效</td>
</tr>
<tr>
<td valign="top" width="269">TIME_CHECK_PLAUSIBILITY</td>
<td valign="top" width="274">检查时间有效性</td>
<td valign="top" width="170">SY-SUBRC = 0 有效</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#HOLIDAY_CHECK_AND_GET_INFO">HOLIDAY_CHECK_AND_GET_INFO</a></td>
<td valign="top" width="274">判断某天是否是假日</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269">&nbsp;</td>
<td valign="top" width="274">&nbsp;</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#CONVERT_DATE_TO_INTERNAL">CONVERT_DATE_TO_INTERNAL</a></td>
<td valign="top" width="274">将标准日期格式转换为数字格式</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#CONVERT_DATE_TO_EXTERNAL">CONVERT_DATE_TO_EXTERNAL</a></td>
<td valign="top" width="274">将数字日期格式转换为系统日期格式</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#DATE_CONVERT_TO_FACTORYDATE">DATE_CONVERT_TO_FACTORYDATE</a></td>
<td valign="top" width="274">将查询日期转为工厂日历日期</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269">&nbsp;</td>
<td valign="top" width="274">&nbsp;</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#HR_99S_INTERVAL_BETWEEN_DATES">HR_99S_INTERVAL_BETWEEN_DATES</a></td>
<td valign="top" width="274">获取两日期间的：天数、周数、月数、年数、期间月份开始截止日期</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#FIMA_DAYS_AND_MONTHS_AND_YEARS">FIMA_DAYS_AND_MONTHS_AND_YEARS</a></td>
<td valign="top" width="274">计算两日期之间间隔的天数</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#FIMA_DATE_COMPARE">FIMA_DATE_COMPARE</a></td>
<td valign="top" width="274">比较两个日期大小</td>
<td valign="top" width="170">返回：GT、EQ、LT等</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#SD_DATETIME_DIFFERENCE">SD_DATETIME_DIFFERENCE</a></td>
<td valign="top" width="274">两日期的时间差</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#HRVE_CONVERT_TIME">HRVE_CONVERT_TIME</a></td>
<td valign="top" width="274">时间：12/24小时制转换</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269">&nbsp;</td>
<td valign="top" width="274">&nbsp;</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#MONTH_NAMES_GET">MONTH_NAMES_GET</a></td>
<td valign="top" width="274">获取所有的月份名字</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#WEEKDAY_GET">WEEKDAY_GET</a></td>
<td valign="top" width="274">获取所有的星期名字</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#DAY_ATTRIBUTES_GET">DAY_ATTRIBUTES_GET</a></td>
<td valign="top" width="274">查看日期属性 包括：该日期是星期几，第几天（周2=2），是不是公共假期等</td>
<td valign="top" width="170">需要输入国家日历</td>
</tr>
<tr>
<td valign="top" width="269">&nbsp;</td>
<td valign="top" width="274">&nbsp;</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#F4_DATE">F4_DATE</a></td>
<td valign="top" width="274">弹出日历对话框，供用户选择日期</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#F4_CLOCK">F4_CLOCK</a></td>
<td valign="top" width="274">弹出时间对话框，供用户选择时间</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269">&nbsp;</td>
<td valign="top" width="274">&nbsp;</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#POPUP_TO_SELECT_MONTH">POPUP_TO_SELECT_MONTH</a></td>
<td valign="top" width="274">弹出选择年和月的对话框</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#POPUP_CALENDAR_SDB">POPUP_CALENDAR_SDB</a></td>
<td valign="top" width="274">弹出选择周的对话框</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269">&nbsp;</td>
<td valign="top" width="274">&nbsp;</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269" style="text-decoration:underline;"><a href="#GET_CURRENT_YEAR">GET_CURRENT_YEAR</a></td>
<td valign="top" width="274">获得当前的财政年（fiscal year）</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269">FIRST_DAY_IN_PERIOD_GET</td>
<td valign="top" width="274">获得期间首日</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="269">LAST_DAY_IN_PERIOD_GET</td>
<td valign="top" width="274">获得期间末日</td>
<td valign="top" width="170">&nbsp;</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h2><b>函数操作说明：</b></h2>
<p><b><a name="FIMA_DATE_CREATE">FIMA_DATE_CREATE 函数</a></b></p>
<p>获取输入日期前、后的年、月、日</p>
<pre class="brush: abap;">

DATA: date TYPE vtbbewe-dvalut,
      flag TYPE trff_flg,
      days TYPE trff_type_n_2.

CALL FUNCTION 'FIMA_DATE_CREATE'
  EXPORTING
    i_date                  = '20140101' "输入日期
    i_flg_end_of_month      = ' '
    i_years                 = 2       "两年后的日期. 即输入日期的年加2所得日期，可为负数，表示前两年
    i_months                = 1       "一个月后的日期.即输入日期的月加1所得日期，可为负数，表示前一个月
    i_days                  = 23      "23天后的日期。可为负数，表示23天前的日期
    i_calendar_days         = 10      "10天后的日历。同I_DAYS参数。
    i_set_last_day_of_month = 'X'     "返回的日期为当前月份的最后一天
  IMPORTING
    e_date                  = date    "返回的日期为当前月份的最后一天
    e_flg_end_of_month      = flag    "如果输入参数I_SET_LAST_DAY_OF_MONTH设置’X’了，则返回值为’X’。
    e_days_of_i_date        = days.   "返回输入日期的i_calendar_days字段与I_DAYS字段的和。
</pre>
<p>输出结果：</p>
<p>date：2016.03.31</p>
<p>flag：X</p>
<p>days：24</p>
<p>&nbsp;</p>
<p><b><a name="RP_CALC_DATE_IN_INTERVAL">RP_CALC_DATE_IN_INTERVAL 函数</a></b></p>
<p>获取输入日期前、后的年、月、日</p>
<pre class="brush: abap;">
DATA calc_date TYPE p0001-begda.

CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
  EXPORTING
    date      = '20140101'    "输入日期
    days      = 10            "天数
    months    = 0             "月数
    signum    = '+'           "+号：表示 N天/月/年后的日期， -号：表示过去的日期
    years     = 0             "年数
  IMPORTING
    calc_date = calc_date.    "返回结果：10天后的日期（2014.01.11）

</pre>
<p>&nbsp;</p>
<p><b><a name="LAST_DAY_OF_MONTHS">LAST_DAY_OF_MONTHS 函数</a></b></p>
<p>获取输入日期最后一天的日期</p>
<pre class="brush: abap;">

DATA date TYPE sy-datum.

CALL FUNCTION 'LAST_DAY_OF_MONTHS'
  EXPORTING
    day_in            = '20140101'     "输入日期
  IMPORTING
    last_day_of_month = date           "返回日期：20140131
  EXCEPTIONS
    day_in_no_date    = 1
    OTHERS            = 2.

</pre>
<p>&nbsp;</p>
<p><b><a name="RP_LAST_DAY_OF_MONTHS">RP_LAST_DAY_OF_MONTHS 函数</a></b></p>
<p>获取输入日期最后一天的日期</p>
<pre class="brush: abap;">

DATA date TYPE sy-datum.

CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
  EXPORTING
    day_in            = '20140101'     "输入日期
  IMPORTING
    last_day_of_month = date           "返回日期：20140131
  EXCEPTIONS
    day_in_no_date    = 1
    OTHERS            = 2.

</pre>
<p>&nbsp;</p>
<p><b><a name="BKK_GET_MONTH_LASTDAY">BKK_GET_MONTH_LASTDAY 函数</a></b></p>
<p>获取输入日期最后一天的日期</p>
<pre class="brush: abap;">

DATA date TYPE sy-datum.

CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'
  EXPORTING
    i_date = '20140101'   "输入日期
  IMPORTING
    e_date = date.        "返回日期：20140131

</pre>
<p>&nbsp;</p>
<p><b><a name="CCM_GO_BACK_MONTHS">CCM_GO_BACK_MONTHS 函数</a></b></p>
<p>获取输入日期过去N月的日期</p>
<pre class="brush: abap;">

DATA date TYPE sy-datum.

CALL FUNCTION 'CCM_GO_BACK_MONTHS'
  EXPORTING
    currdate   = '20140101'   "输入日期
    backmonths = 3            "过去月数
  IMPORTING
    newdate    = date.        "返回日期：20131001

</pre>
<p>&nbsp;</p>
<p><b><a name="MONTH_PLUS_DETERMINE">MONTH_PLUS_DETERMINE 函数</a></b></p>
<p>获取输入日期后来N月的日期</p>
<pre class="brush: abap;">

DATA date TYPE sy-datum.

CALL FUNCTION 'MONTH_PLUS_DETERMINE'
  EXPORTING
    months  = 3
    olddate = '20140101'   "输入日期
  IMPORTING
    newdate = date.        "返回日期：20140401

</pre>
<p>&nbsp;</p>
<p><b><a name="DATE_GET_WEEK">DATE_GET_WEEK 函数</a></b></p>
<p>获取输入日期该年的周数，输出格式为：YYYYWW。</p>
<pre class="brush: abap;">

DATA week TYPE scal-week.

CALL FUNCTION 'DATE_GET_WEEK'
  EXPORTING
    date         = '20140101'   "输入日期
  IMPORTING
    week         = week         "返回周数:201401
  EXCEPTIONS
    date_invalid = 1
    OTHERS       = 2.

</pre>
<p>&nbsp;</p>
<p><b><a name="WEEK_GET_FIRST_DAY">WEEK_GET_FIRST_DAY 函数</a></b></p>
<p>获取输入周数的第一天</p>
<pre class="brush: abap;">

DATA date TYPE sy-datum.

CALL FUNCTION 'WEEK_GET_FIRST_DAY'
  EXPORTING
    week         = '201401'   "输入周
  IMPORTING
    date         = date       "返回该周第一天(周一)：2013.12.30
  EXCEPTIONS
    week_invalid = 1
    OTHERS       = 2.

</pre>
<p>&nbsp;</p>
<p><b><a name="DAY_IN_WEEK">DAY_IN_WEEK 函数</a></b></p>
<p>获取输入日期该天是星期几/周几</p>
<pre class="brush: abap;">
DATA wotnr TYPE p.

CALL FUNCTION 'DAY_IN_WEEK'
  EXPORTING
    datum = '20140101'  "输入日期
  IMPORTING
    wotnr = wotnr.      "返回：3 =》星期三/周三

</pre>
<p>&nbsp;</p>
<p><b><a name="GET_WEEK_INFO_BASED_ON_DATE">GET_WEEK_INFO_BASED_ON_DATE 函数</a></b></p>
<p>获取输入日期该年的周数和这周周一、周日日期（周起始日是周一）</p>
<pre class="brush: abap;">

DATA: week   TYPE scal-week ,
      monday TYPE sy-datum,
      sunday TYPE sy-datum.

CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'
  EXPORTING
    date   = '20140101'
  IMPORTING
    week   = week       "返回周数：201401
    monday = monday     "返回周一日期：2013.12.30
    sunday = sunday.    "返回周日日期：2014.01.05

</pre>
<p>&nbsp;</p>
<p><b><a name="HR_GBSSP_GET_WEEK_DATES">HR_GBSSP_GET_WEEK_DATES 函数</a></b></p>
<p>获取查询日期该年的第几周和这周周一、周日日期，该天是星期几/周几（周起始日是周日）</p>
<pre class="brush: abap;">

DATA: sunday      TYPE sy-datum,
      saturday    TYPE sy-datum,
      day_in_week TYPE i,
      week_no     TYPE p08_weekno.

CALL FUNCTION 'HR_GBSSP_GET_WEEK_DATES'
  EXPORTING
    p_pdate       = '20140101'    "输入日期
  IMPORTING
    p_sunday      = sunday        "返回本周开始日期（周日）：2013.12.29
    p_saturday    = saturday      "返回本周结束日期（周六）：2014.01.04
    p_day_in_week = day_in_week   "返回该日星期几/周几: 4 => 星期三/周三
                                  "(这周中的第4天，由于开始日期是周日，故需要-1)
    p_week_no     = week_no.      "返回本周开始日期的周数：201352

</pre>
<p>&nbsp;</p>
<p><b><a name="HOLIDAY_CHECK_AND_GET_INFO">HOLIDAY_CHECK_AND_GET_INFO 函数</a></b></p>
<p>判断某天是否是假日</p>
<pre class="brush: abap;">

DATA holiday TYPE c.
DATA attributes TYPE SORTED TABLE OF thol.

CALL FUNCTION 'HOLIDAY_CHECK_AND_GET_INFO'
  EXPORTING
    date                         = '20140201'  "输入日期:春节
    holiday_calendar_id          = 'ZS'        "假日日历 ID
    with_holiday_attributes      = ' '         "属性标识
  IMPORTING
    holiday_found                = holiday     "输出是否为假日的标记  输出结果：X
  TABLES
    holiday_attributes           = attributes  "输出该假日的属性
                                               "若WITH_HOLIDAY_ATTRIBUTES参数 = X，则会带出属性记录
  EXCEPTIONS
    calendar_buffer_not_loadable = 1
    date_after_range             = 2
    date_before_range            = 3
    date_invalid                 = 4
    holiday_calendar_id_missing  = 5
    holiday_calendar_not_found   = 6
    OTHERS                       = 7.

</pre>
<p>&nbsp;</p>
<p><b><a name="CONVERT_DATE_TO_INTERNAL">CONVERT_DATE_TO_INTERNAL 函数</a></b></p>
<p>将标准日期格式转换为内部数字格式</p>
<p>日期的格式与用户参数有关，转化为内部数字格式时，都为：YYYYMMDD.</p>
<pre class="brush: abap;">

DATA date TYPE sy-datum.

CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
  EXPORTING
    date_external            = '2014.01.01' "当前用户日期格式：YYYY.MM.DD
    accept_initial_date      = ' '
  IMPORTING
    date_internal            = date         "输出20140101
  EXCEPTIONS
    date_external_is_invalid = 1
    OTHERS                   = 2.

</pre>
<p>&nbsp;</p>
<p><b><a name="CONVERT_DATE_TO_EXTERNAL">CONVERT_DATE_TO_EXTERNAL 函数</a></b></p>
<p>将内部数字日期格式转换为系统日期格式</p>
<p>系统日期格式输出时，与用户参数设置有关</p>
<pre class="brush: abap;">

DATA date2(10) TYPE c.

CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
  EXPORTING
    date_internal            = '20140101'   
  IMPORTING
    date_external            = date2     "根据用户日期格式参数输出:YYYY.MM.DD
  EXCEPTIONS
    date_internal_is_invalid = 1
    OTHERS                   = 2.

</pre>
<p>&nbsp;</p>
<p><b><a name="DATE_CONVERT_TO_FACTORYDATE">DATE_CONVERT_TO_FACTORYDATE 函数</a></b></p>
<p>根据日期返回工厂日历日期</p>
<pre class="brush: abap;">

DATA: date        TYPE scal-date,
      factorydate TYPE facdate,
      workingday  TYPE scal-indicator.

CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE'
  EXPORTING
    correct_option               = '+'            "表示工作日计算方式的标记
    date                         = '20140101'     "必须转换的日期
    factory_calendar_id          = 'ZS'           "工厂日历ID
  IMPORTING
    date                         = date           "工厂日历日期
    factorydate                  = factorydate    "指定日历中的工作日数
    workingday_indicator         = workingday     "表示某日期是否为工作日的标志
  EXCEPTIONS
    calendar_buffer_not_loadable = 1
    correct_option_invalid       = 2
    date_after_range             = 3
    date_before_range            = 4
    date_invalid                 = 5
    factory_calendar_not_found   = 6
    OTHERS                       = 7.
</pre>
<p>输出结果：<br />
date:           20140102<br />
factorydate:    1531<br />
workingday:     +</p>
<p>&nbsp;</p>
<p><b><a name="HR_99S_INTERVAL_BETWEEN_DATES">HR_99S_INTERVAL_BETWEEN_DATES 函数</a></b></p>
<p>获取两个日期期间的：天数、周数、月数、年数；</p>
<p>包括期间月份的开始(月份第一天日期)、截止日期（月份最后一天日期）</p>
<pre class="brush: abap;">

TYPE-POOLS: p99sg.
DATA: days   TYPE i,
      weeks  TYPE i,
      months TYPE i,
      years  TYPE i.

DATA: month_tab TYPE STANDARD TABLE OF p99sg_month_tab_row.

CALL FUNCTION 'HR_99S_INTERVAL_BETWEEN_DATES'
  EXPORTING
    begda     = '20130101'    "开始日期
    endda     = '20140101'    "截止日期
  IMPORTING
    days      = days          "返回天数：266
    c_weeks   = weeks         "返回周数：52
    c_months  = months        "返回月数：12
    c_years   = years         "返回年数：1
    month_tab = month_tab.    "期间月份首尾日

</pre>
<p>month_tab 内容明细：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/03/abap_function_datetime_month_table_detail.png" alt="abap_function_datetime_month_table_detail" width="253" height="288" class="alignnone size-full wp-image-306" /></p>
<p>&nbsp;</p>
<p><b><a name="FIMA_DAYS_AND_MONTHS_AND_YEARS">FIMA_DAYS_AND_MONTHS_AND_YEARS 函数</a></b></p>
<p>计算两个日期期间的天数</p>
<pre class="brush: abap;">

DATA: days   TYPE i,
      months TYPE i,
      years  TYPE i.

CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
  EXPORTING
    i_date_from    = '20130101'
    i_date_to      = '20141001'
    i_flg_separate = ' '       "是否单独计算 若有标识，则返回两日期相差的天数
  IMPORTING
    e_days         = days      "返回天数：638
    e_months       = months    "返回月数：21
    e_years        = years.    "返回年数：2

* 单独计算
CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
  EXPORTING
    i_date_from    = '20130101'
    i_date_to      = '20141001'
    i_flg_separate = 'X'       "标识 'X'
  IMPORTING
    e_days         = days      "返回天数：0
    e_months       = months    "返回月数：9
    e_years        = years.    "返回年数：1

</pre>
<p>&nbsp;</p>
<p><b><a name="FIMA_DATE_COMPARE">FIMA_DATE_COMPARE 函数</a></b></p>
<p>比较两个日期大小，返回结果是关系操作符：<b>GT、EQ、LT</b></p>
<pre class="brush: abap;">

TYPE-POOLS trff .

DATA log_op TYPE trff_type_c_2.

CALL FUNCTION 'FIMA_DATE_COMPARE'
  EXPORTING
    i_flg_intraday = ' '
    i_date         = '20140101'
    i_time         = '000000'
    i_sincl        = '0'          "计算期间开始的排除标识
    i_sult         = ' '          "计算期间开始的月末标识符
    i_comp_date    = '20140201'   "比较日期
    i_comp_time    = '000000'     "比较时间
    i_comp_sincl   = '0'          "计算期间结束的包含标识
    i_comp_sult    = ' '          "计算期间结束的月末标识符
  IMPORTING
    e_log_op       = log_op.      "返回比较结果：LT

</pre>
<p>&nbsp;</p>
<p><b><a name="SD_DATETIME_DIFFERENCE">SD_DATETIME_DIFFERENCE 函数</a></b></p>
<p>两个日期作差，即是两个日期相减，包括当天时间。</p>
<pre class="brush: abap;">

DATA: datediff TYPE p,
      timediff TYPE p,
      earliest TYPE c.

CALL FUNCTION 'SD_DATETIME_DIFFERENCE'
  EXPORTING
    date1            = '20140101'
    time1            = '240000'
    date2            = '20140101'
    time2            = '083000'
  IMPORTING
    datediff         = datediff     "返回日期差：0
    timediff         = timediff     "返回时间差：16
    earliest         = earliest     "返回时间正负：2，1-负 0-相等 2-正
  EXCEPTIONS
    invalid_datetime = 1
    OTHERS           = 2.

</pre>
<p>&nbsp;</p>
<p><b><a name="HRVE_CONVERT_TIME">HRVE_CONVERT_TIME 函数</a></b></p>
<p>12小时制与24小时制的时间转换，例如：07:00:00 pm -> 19:00:00。</p>
<pre class="brush: abap;">
DATA: lv_in_time  TYPE tims,
      lv_out_time TYPE tims,
      lv_am_pm    TYPE c.
lv_in_time = '060000'.
CALL FUNCTION 'HRVE_CONVERT_TIME'
  EXPORTING
    type_time       = 'B'    " A = 24小时制 -> 12小时制  B = 12小时制 -> 24小时制
    input_time      = lv_in_time
    input_am_pm     = 'PM'
  IMPORTING
    output_time     = lv_out_time
    output_am_pm    = lv_am_pm
  EXCEPTIONS
    parameter_error = 1
    OTHERS          = 2.

WRITE:/ | Input Time - { lv_in_time }|.    " 输出：060000
WRITE:/ |Output Time - { lv_out_time }|.   " 输出：180000
</pre>
<p>&nbsp;</p>
<p><b><a name="MONTH_NAMES_GET">MONTH_NAMES_GET 函数</a></b></p>
<p>获取所有的月份名字信息</p>
<pre class="brush: abap;">

DATA return_code TYPE sy-subrc.

DATA month_names TYPE STANDARD TABLE OF t247.

CALL FUNCTION 'MONTH_NAMES_GET'
  EXPORTING
    language              = sy-langu       "系统当前语言
  IMPORTING
    return_code           = return_code    "返回 SY-SUBRC
  TABLES
    month_names           = month_names    "返回月份信息
  EXCEPTIONS
    month_names_not_found = 1
    OTHERS                = 2.

</pre>
<p>month_names 明细内容：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/03/abap_function_datetime_month_names_detail.png" alt="abap_function_datetime_month_names_detail" width="181" height="302" class="alignnone size-full wp-image-305" /></p>
<p>&nbsp;</p>
<p><b><a name="WEEKDAY_GET">WEEKDAY_GET 函数</a></b></p>
<p>获取所有的星期名字</p>
<pre class="brush: abap;">

DATA return_code TYPE sy-subrc.

DATA weekday TYPE STANDARD TABLE OF t246.

CALL FUNCTION 'WEEKDAY_GET'
  EXPORTING
    language          = sy-langu      "系统当前语言
  IMPORTING
    return_code       = return_code   "返回 SY-SUBRC
  TABLES
    weekday           = weekday       "返回星期名字信息
  EXCEPTIONS
    weekday_not_found = 1
    OTHERS            = 2.

</pre>
<p>weekday 内容明细：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/03/abap_function_datetime_weekday_detail.png" alt="abap_function_datetime_weekday_detail" width="241" height="217" class="alignnone size-full wp-image-304" /></p>
<p>&nbsp;</p>
<p><b><a name="DAY_ATTRIBUTES_GET">DAY_ATTRIBUTES_GET 函数</a></b></p>
<p>查看日期属性 包括：该日期是星期几，第几天，是否公共假期等；</p>
<p>公共假期等内容需要输入国家日历。</p>
<pre class="brush: abap;">

DATA: year_of_valid_from TYPE scal-year,
      year_of_valid_to   TYPE scal-year,
      return_code        TYPE sy-subrc.

DATA day_attributes TYPE STANDARD TABLE OF casdayattr.

CALL FUNCTION 'DAY_ATTRIBUTES_GET'
  EXPORTING
    factory_calendar           = ' '              "工厂日历ID
    holiday_calendar           = ' '              "假期日历ID
    date_from                  = '20140101'       "开始日期
    date_to                    = '20140110'       "截止日期
    language                   = sy-langu         "系统当前语言
    non_iso                    = ' '
  IMPORTING
    year_of_valid_from         = year_of_valid_from
    year_of_valid_to           = year_of_valid_to
    returncode                 = return_code
  TABLES
    day_attributes             = day_attributes "返回日期每日属性
  EXCEPTIONS
    factory_calendar_not_found = 1
    holiday_calendar_not_found = 2
    date_has_invalid_format    = 3
    date_inconsistency         = 4
    OTHERS                     = 5.

</pre>
<p>day_attributes 内容明细：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/03/abap_function_datetime_day_attributes_detail.png" alt="abap_function_datetime_day_attributes_detail" width="888" height="267" class="alignnone size-full wp-image-303" /></p>
<p>&nbsp;</p>
<p><b><a name="F4_DATE">F4_DATE 函数</a></b></p>
<p>为 F4 帮助显示日历，弹出日历对话框，供用户选择日期</p>
<pre class="brush: abap;">

PARAMETERS:p1(6) TYPE c.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p1.

  DATA:l_date TYPE sy-datum.

  CALL FUNCTION 'F4_DATE'
    EXPORTING
      date_for_first_month         = sy-datum
    IMPORTING
      select_date                  = l_date     "用户选择后返回的日期
    EXCEPTIONS
      calendar_buffer_not_loadable = 1
      date_after_range             = 2
      date_before_range            = 3
      date_invalid                 = 4
      factory_calendar_not_found   = 5
      holiday_calendar_not_found   = 6
      parameter_conflict           = 7
      OTHERS                       = 8.

</pre>
<p>显示效果：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/03/abap_function_datetime_f4_date_demo.png" alt="abap_function_datetime_f4_date_demo" width="362" height="417" class="alignnone size-full wp-image-302" /></p>
<p>&nbsp;</p>
<p><b><a name="F4_CLOCK">F4_CLOCK 函数</a></b></p>
<p>为 F4 帮助显示时间，弹出时间对话框，供用户选择时间</p>
<pre class="brush: abap;">

PARAMETERS:p1(6) TYPE c.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p1.

  DATA: l_time TYPE sy-uzeit.

  CALL FUNCTION 'F4_CLOCK'
    EXPORTING
      start_time    = sy-uzeit
      display       = ' '
    IMPORTING
      selected_time = l_time.

</pre>
<p>效果显示：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/03/abap_function_datetime_f4_clock_demo.png" alt="abap_function_datetime_f4_clock_demo" width="335" height="191" class="alignnone size-full wp-image-301" /></p>
<p>&nbsp;</p>
<p><b><a name="POPUP_TO_SELECT_MONTH">POPUP_TO_SELECT_MONTH 函数</a></b></p>
<p>为 F4 帮助显示月份：弹出选择年和月的对话框</p>
<pre class="brush: abap;">

PARAMETERS: p_year   TYPE mard-lfgja DEFAULT sy-datum+0(4),
            p_month  TYPE mard-lfmon.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_month.

  DATA: actual_month   LIKE isellist-month,
        selected_month LIKE isellist-month,
        return_code    TYPE sy-subrc.

  CONCATENATE p_year sy-datum+4(2) INTO actual_month.

  CALL FUNCTION 'POPUP_TO_SELECT_MONTH'
    EXPORTING
      actual_month               = actual_month   "传入年份
      factory_calendar           = ' '            "工厂日历 ID
      holiday_calendar           = ' '            "假日日历 ID
      language                   = sy-langu       "当前系统语言
      start_column               = 8              "弹出框屏幕位置
      start_row                  = 5              "弹出框屏幕位置
    IMPORTING
      selected_month             = selected_month "返回用户输入月份
      return_code                = return_code    "返回 sy-subrc
    EXCEPTIONS
      factory_calendar_not_found = 1
      holiday_calendar_not_found = 2
      month_not_found            = 3
      OTHERS                     = 4.

  IF return_code = 0.

    p_year  = selected_month+0(4).
    p_month = selected_month+4(2).

  ENDIF.

</pre>
<p>效果显示：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/03/abap_function_datetime_f4_month_demo.png" alt="abap_function_datetime_f4_month_demo" width="316" height="183" class="alignnone size-full wp-image-300" /></p>
<p>&nbsp;</p>
<p><b><a name="POPUP_CALENDAR_SDB">POPUP_CALENDAR_SDB 函数</a></b></p>
<p>弹出选择周的对话框</p>
<pre class="brush: abap;">

DATA: begin_date TYPE sy-datum,
      end_date   TYPE sy-datum.

CALL FUNCTION 'POPUP_CALENDAR_SDB'
  EXPORTING
    sel_day      = 'X'        "启用天 选择
    sel_week     = 'X'        "启用周 选择
    sel_month    = 'X'        "启用月 选择
    sel_interval = 'X'
    focus_day    = sy-datum   "默认选择日期
  IMPORTING
    begin_date   = begin_date "返回选择周第一天
    end_date     = end_date.  "返回选择周最后一天

</pre>
<p>效果显示：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/03/abap_function_datetime_f4_week_demo.png" alt="abap_function_datetime_f4_week_demo" width="270" height="668" class="alignnone size-full wp-image-299" /></p>
<p>&nbsp;</p>
<p><b><a name="GET_CURRENT_YEAR">GET_CURRENT_YEAR 函数</a></b></p>
<p>获得当前的财政年（Fiscal year）</p>
<pre class="brush: abap;">

DATA: currm TYPE bkpf-monat,
      curry TYPE bkpf-gjahr,
      prevm TYPE bkpf-monat,
      prevy TYPE bkpf-gjahr.

CALL FUNCTION 'GET_CURRENT_YEAR'
  EXPORTING
    bukrs = '3010'      "输入公司代码
    date  = sy-datum    "输入日期
  IMPORTING
    currm = currm       "当前会计期间
    curry = curry       "当前会计年度
    prevm = prevm       "上期会计期间
    prevy = prevy.      "上期会计年度

</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>